﻿using Microsoft.Practices.Unity.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Artech.Core
{
    public class ServiceLocatableObjectCollection<T> 
    {
        public ServiceLocatableObjectCollection(IServiceLocator serviceLocator = null)
        {
            this.ServiceLocator = serviceLocator ?? ServiceLocatorBuilder.Current.GetServiceLocator();
        }

        public IServiceLocator ServiceLocator { get; private set; }

        public bool ContainsKey(string key)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            return this.ServiceLocator.CanLocateService<T>(key);
        }

        public bool TryGetValue(string key, out T value)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            if (this.ContainsKey(key))
            {
                value =  this.ServiceLocator.GetService<T>(key);
                return true;
            }
            value = default(T);
            return false;
        }

        public T this[string name]
        {
            get
            {
                T value;
                if(this.TryGetValue(name, out value))
                {
                    return value;
                }
                return default(T);
            }
        }
    }
}
